//
//  XDLMSanPostThreeTableViewCell.m
//  xindailianmeng
//
//  Created by 许佳奇 on 17/2/20.
//  Copyright © 2017年 xindailianmeng.cn. All rights reserved.
//

#import "XDLMSanPostThreeTableViewCell.h"
#import "XDLMSanPostFourTableViewCell.h"
#define TCOLOR [UIColor colorWithRed:187.0/255.0 green:147.0/255.0 blue:115.0/255.0 alpha:1]
@interface XDLMSanPostThreeTableViewCell()<UITableViewDelegate,UITableViewDataSource>
@end
@implementation XDLMSanPostThreeTableViewCell

{
    UILabel *_titleLab;
    UIImageView *_imgView;
    UIView *_newbgView;
    UITableView *_table;
    NSArray *_dataArray;
}
- (void)awakeFromNib {
    // Initialization code
}
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self setUI];
    }
    return self;
}
- (void)setUI
{
    _titleLab = [self createLabelWithFrame:CGRectMake(15, 10, 100*kscale, 30*kscale) text:@"" textFont:[UIFont systemFontOfSize:16] textColor:[UIColor colorWithRed:102.0/255.0 green:102.0/255.0 blue:102.0/255.0 alpha:1]];
    _titleLab.textAlignment = NSTextAlignmentLeft;
    
    
    
    _btn = [[UIButton alloc] initWithFrame:CGRectMake(kScreenW - 15- 9*kscale, 17*kscale, 6, 10)];
    [_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    _btn.selected = NO;
    [_btn addTarget:self action:@selector(btnCilck:) forControlEvents:UIControlEventTouchUpInside];
    
    [_btn setBackgroundImage:[UIImage imageNamed:@"rightcell"] forState:UIControlStateNormal];
    [self addSubview:_btn];
    _bgView = [[UIView alloc] init];
    _bgView.frame = CGRectMake(0, 0, 300, 300);
    [_bgView setHidden:YES];
    [self addSubview:_bgView];
    UILabel *lineLab = [self createLabelWithFrame:CGRectMake(15, 49*kscale, kScreenW - 30, 0.5) text:@"" textFont:nil textColor:nil];
    lineLab.backgroundColor = [UIColor colorWithRed:238.0/255.0 green:238.0/255.0 blue:238.0/255.0 alpha:1];
    [_bgView addSubview:lineLab];
    _table = [[UITableView alloc] initWithFrame:CGRectMake(0, 50*kscale, kScreenW, 350*kscale)];
    _table.dataSource = self;
    _table.delegate = self;
    [_table registerClass:[XDLMSanPostFourTableViewCell class] forCellReuseIdentifier:@"XDLMSanPostFourTableViewCell"];
    [_bgView addSubview:_table];
    [self addSubview:_imgView];
    [self addSubview:_titleLab];
    
    
    
}
- (void)reloadData:(NSArray *)data
{
    if (data.count < 5) {
        _dataArray = data;
        if (_dataArray.count == 0) {
            [_btn setEnabled:NO];
            [_btn setTitle:@"无记录" forState:UIControlStateNormal];
        }
        else
        {
            [_btn setEnabled:YES];
        }
        _titleLab.text = _title;
        [_table reloadData];
    }
    
}

-(void)btnCilck:(UIButton *)sender
{
    _btn.selected = !_btn.selected;
    if (sender.selected) {
        //展开的布局
        _btn.frame = CGRectMake(kScreenW - 15- 9*kscale, 17*kscale, 10, 6);
        [_btn setBackgroundImage:[UIImage imageNamed:@"xiala"] forState:UIControlStateSelected];
        if (self.isSelsct) {
            self.isSelsct(YES);
        }
        [_bgView setHidden:NO];
        
    }
    else
    {
        
        if (self.isSelsct) {
            self.isSelsct(NO);
        }
        _btn.frame = CGRectMake(kScreenW - 15- 9*kscale, 17*kscale, 6, 10);
        [_btn setBackgroundImage:[UIImage imageNamed:@"rightcell"] forState:UIControlStateNormal];
        [_bgView setHidden:YES];
    }
    
    
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell1"];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15*kscale, 12*kscale, 150, 14)];
            [label setFont:[UIFont systemFontOfSize:14]];
            [label setText:@"投资人/时间"];
            [cell addSubview:label];
            
            UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(kScreenW - 100, 12*kscale, 85, 14)];
            [label1 setTextAlignment:NSTextAlignmentRight];
            [label1 setFont:[UIFont systemFontOfSize:14]];
            [label1 setText:@"金额(元)"];
            [cell addSubview:label1];
            
        }
        return cell;
    }
    else if (indexPath.row == _dataArray.count +1)
    {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell2"];
            [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
            UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, kScreenW, 50*kscale)];
            [lab setTextAlignment:NSTextAlignmentCenter];
            [lab setFont:[UIFont systemFontOfSize:13]];
            [lab setTextColor:[UIColor blackColor]];
            [lab setText:@"点击查看更多"];
            [cell addSubview:lab];
        }
        
        
        return cell;
    }
    else
    {
        XDLMSanPostFourTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XDLMSanPostFourTableViewCell" forIndexPath:indexPath];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        if (indexPath.row > 0 && indexPath.row<5) {
            [cell setDataDic:_dataArray[indexPath.row-1]];
        }
        
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == _dataArray.count +1) {
        
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count+2;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 39*kscale;
    }
    else if (indexPath.row == _dataArray.count +1)
    {
        return 50*kscale;
    }
    else
    {
        return 67*kscale;
    }
}
//创建Label
- (UILabel *)createLabelWithFrame:(CGRect)frame text:(NSString *)text textFont:(UIFont *)font textColor:(UIColor *)color {
    
    UILabel * label = [[UILabel alloc] initWithFrame:frame];
    label.text = text;
    label.textColor = color;
    label.font = font;
    label.adjustsFontSizeToFitWidth = YES;
    label.textAlignment = NSTextAlignmentCenter;
    
    return label;
    
}
-(NSMutableArray *)dataSource
{
    if (_dataSource== nil) {
        _dataSource = [[NSMutableArray alloc] init];
    }
    return _dataSource;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
